home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_206 / brownian / brownian.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  9KB  |  366 lines

  1. /****************************************************************
  2.  * fault fractals, by John M. Olsen
  3.  ****************************************************************/
  4.  
  5. #include <math.h>
  6. #include <graphics/gfxbase.h>
  7. #include <graphics/gfxmacros.h>
  8. #include <graphics/rastport.h>
  9. #include <hardware/blit.h>
  10. #include <hardware/custom.h>
  11. #include <intuition/intuition.h>
  12.  
  13. /* Only one of the following two should be defined. */
  14. /* #define NOCYCLE  /* define this to make it not color cycle. */
  15. #define CYCLE     /* define this to make it color cycle. */
  16.  
  17. #define WIDTH  320
  18. #define HEIGHT 200
  19. #define BASE   0
  20.  
  21. int trash;
  22. long mybltsize, offset, minbltsize;
  23. void *OpenLibrary();
  24. struct IntuiMessage *GetMsg(), *msg;
  25. struct IntuitionBase *IntuitionBase;
  26. struct GfxBase *GfxBase;
  27. struct Screen *s, *s1, *OpenScreen();
  28. struct Window *w, *w1, *w2, *w3, *OpenWindow();
  29. BYTE *AllocRaster();
  30. WORD areabuffer[250];
  31. struct TmpRas tmpras;
  32. struct AreaInfo myareainfo;
  33.  
  34. struct NewScreen ss =
  35. {
  36.     0,0,320,200,5,0,1, /* 5 planes */
  37.     NULL,CUSTOMSCREEN,NULL,(UBYTE *)"  Fractal Brownian Motion",
  38.     NULL,NULL
  39. };
  40.  
  41. struct NewScreen ss1 =
  42. {
  43.     0,0,320,200,2,0,1, /* 2 planes */
  44.     NULL,CUSTOMSCREEN,NULL,(UBYTE *)"  Sneaky Blitter Work Area",
  45.     NULL,NULL
  46. };
  47.  
  48. struct NewWindow ww =
  49. {
  50.     0,0,320,200,-1,-1,MOUSEBUTTONS, /* IDCMP */
  51.     NOCAREREFRESH | BACKDROP | BORDERLESS | RMBTRAP,
  52.     NULL,NULL,(UBYTE *)NULL,
  53.     NULL,NULL,0,0,0,0,CUSTOMSCREEN
  54. };
  55.  
  56. struct NewWindow wb =
  57. {
  58.     0,0,14,10,-1,-1, CLOSEWINDOW, /* IDCMP */
  59.     NOCAREREFRESH | WINDOWCLOSE | BORDERLESS | RMBTRAP,
  60.     NULL,NULL,(UBYTE *)NULL,
  61.     NULL,NULL,0,0,0,0,CUSTOMSCREEN
  62. };
  63.  
  64. struct NewWindow wb1 =
  65. {
  66.     0,0,14,10,-1,-1, CLOSEWINDOW, /* IDCMP */
  67.     NOCAREREFRESH | WINDOWCLOSE | BORDERLESS | RMBTRAP,
  68.     NULL,NULL,(UBYTE *)NULL,
  69.     NULL,NULL,0,0,0,0,CUSTOMSCREEN
  70. };
  71.  
  72. struct NewWindow ww1 =
  73. {
  74.     0,0,320,200,-1,-1,MOUSEBUTTONS, /* No IDCMP */
  75.     NOCAREREFRESH | BACKDROP | BORDERLESS | RMBTRAP,
  76.     NULL,NULL,(UBYTE *)NULL,
  77.     NULL,NULL,0,0,0,0,CUSTOMSCREEN
  78. };
  79.  
  80. short colormap[32] =
  81. {
  82.     0x000, 0x101, 0x111, 0x212, 0x222, 0x323, 0x333, 0x434,
  83.     0x444, 0x545, 0x555, 0x656, 0x666, 0x767, 0x777, 0x878,
  84.     0x888, 0x989, 0x999, 0xa9a, 0xaaa, 0xbab, 0xbbb, 0xcbc,
  85.     0xccc, 0xdcd, 0xddd, 0xede, 0xeee, 0xfee, 0xfef, 0xfff
  86. };
  87.  
  88. main(argc, argv)
  89. char *argv[];
  90. {
  91.     long count, class, code;
  92.     struct RastPort *r, *r1;
  93.     register long x,y;
  94.     float m,b,theta;
  95.  
  96.     setup();
  97.     r = w->RPort;
  98.     r1 = w1->RPort;
  99.     SetRast(r,(long)BASE);
  100.     while(1)
  101.     {
  102.         if(msg = GetMsg(w->UserPort)) ReplyMsg(msg);
  103.         if(msg = GetMsg(w1->UserPort)) ReplyMsg(msg);
  104.         if(msg = GetMsg(w2->UserPort))
  105.         {
  106.             class = msg->Class;
  107.             ReplyMsg(msg);
  108.             if(class == CLOSEWINDOW) die(0);
  109.         }
  110.         if(msg = GetMsg(w3->UserPort))
  111.         {
  112.             class = msg->Class;
  113.             ReplyMsg(msg);
  114.             if(class == CLOSEWINDOW) die(0);
  115.         }
  116. /* draw mask into plane 0 of s1 */
  117.         SetRast(r1, 0L);
  118.         SetAPen(r1, 1L);
  119.         AreaMove(r1, (long)(rand()%320), (long)(rand()%200));
  120.         AreaDraw(r1, (long)(rand()%320), (long)(rand()%200));
  121.         AreaDraw(r1, (long)(rand()%320), (long)(rand()%200));
  122.         AreaEnd(r1);
  123.         if(count) fracadd(r,w1->RPort->BitMap->Planes[1],
  124.             w1->RPort->BitMap->Planes[0]);
  125.         else fracsub(r,w1->RPort->BitMap->Planes[1],
  126.             w1->RPort->BitMap->Planes[0]);
  127. #ifdef CYCLE
  128.         FudgeColors();
  129. #endif
  130.         count = !count;
  131.     }
  132. }
  133.  
  134. setup()
  135. {
  136.     long sec,mic;
  137.     register long i,j;
  138.  
  139.     if(!(GfxBase = OpenLibrary("graphics.library",0L)))
  140.         die(1);
  141.     if(!(IntuitionBase = OpenLibrary("intuition.library",0L)))
  142.         die(1);
  143.     if(!(s1 = OpenScreen(&ss1)))
  144.         die(2);
  145.     ww1.Screen = s1;
  146.     if(!(w1 = OpenWindow(&ww1)))
  147.         die(3);
  148.     wb1.Screen = s1;
  149.     if(!(w3 = OpenWindow(&wb1)))
  150.         die(3);
  151.     if(!(s = OpenScreen(&ss)))
  152.         die(2);
  153.     ww.Screen = s;
  154.     if(!(w = OpenWindow(&ww)))
  155.         die(3);
  156.     wb.Screen = s;
  157.     if(!(w2 = OpenWindow(&wb)))
  158.         die(3);
  159.     LoadRGB4(&s->ViewPort, colormap, 32L);
  160.  
  161.     InitArea(&myareainfo, areabuffer, 100L);
  162.     w1->RPort->AreaInfo = &myareainfo;
  163.     tmpras.RasPtr = (BYTE *) AllocRaster(320L, 200L);
  164.     tmpras.Size = (long) RASSIZE(320L, 200L);
  165.     w1->RPort->TmpRas = &tmpras;
  166.  
  167.     offset = 10 * w1->RPort->BitMap->BytesPerRow;
  168.      minbltsize = ((w1->RPort->BitMap->Rows - 10) << 6) |
  169.         (w1->RPort->BitMap->BytesPerRow >> 1);
  170.     /* now make the silly title visible.  Colors 1,2,3 -> 8,16,24 */
  171.     for(i = 0; i < 10; i++)
  172.     {
  173.         for(j = 0; j < 320; j++)
  174.         {
  175.             SetAPen(&(s->RastPort), 
  176.                 (long)(8*ReadPixel(&(s->RastPort),j,i)));
  177.             WritePixel(&(s->RastPort), j,i);
  178.         }
  179.     }
  180.     CurrentTime(&sec,&mic);
  181.     srand((int)mic); /* microseconds makes a good random seed. */
  182. }
  183.  
  184. fracadd(r,a,b)
  185. struct RastPort *r;
  186. PLANEPTR a,b;
  187. {
  188.     register int x, newoff;
  189.  
  190.     newoff = offset;
  191.      mybltsize = ((w1->RPort->BitMap->Rows - 10) << 6) |
  192.         (w1->RPort->BitMap->BytesPerRow >> 1);
  193.     OwnBlitter();
  194.     for(x = 0; x < 5; x++) /* for each bitplane */
  195.     {
  196.         WaitBlit();
  197. /* plane a = plane x AND plane b */
  198.         custom.bltapt = (APTR)(b+newoff);
  199.         custom.bltbpt = (APTR)(r->BitMap->Planes[x]+newoff);
  200.         custom.bltdpt = (APTR)(a+newoff);
  201.         custom.bltafwm = 0xffff;
  202.         custom.bltalwm = 0xffff;
  203.         custom.bltamod = 0;
  204.         custom.bltbmod = 0;
  205.         custom.bltdmod = 0;
  206.         custom.bltcon1 = 0;
  207.         custom.bltcon0 = BC0F_SRCA | BC0F_SRCB | BC0F_DEST |
  208.             ABC | ABNC;
  209.         custom.bltsize = mybltsize;
  210. /* plane x = plane x XOR plane b */
  211.         WaitBlit();
  212.         custom.bltapt = (APTR)(b+newoff);
  213.         custom.bltbpt = (APTR)(r->BitMap->Planes[x]+newoff);
  214.         custom.bltdpt = (APTR)(r->BitMap->Planes[x]+newoff);
  215.         custom.bltafwm = 0xffff;
  216.         custom.bltalwm = 0xffff;
  217.         custom.bltamod = 0;
  218.         custom.bltbmod = 0;
  219.         custom.bltdmod = 0;
  220.         custom.bltcon1 = 0;
  221.         custom.bltcon0 = BC0F_SRCA | BC0F_SRCB | BC0F_DEST |
  222.             ANBC | NABC | ANBNC | NABNC;
  223.         custom.bltsize = mybltsize;
  224. /* plane b = plane a */
  225.         WaitBlit();
  226.         custom.bltapt = (APTR)(a+newoff);
  227.         custom.bltdpt = (APTR)(b+newoff);
  228.         custom.bltafwm = 0xffff;
  229.         custom.bltalwm = 0xffff;
  230.         custom.bltamod = 0;
  231.         custom.bltbmod = 0;
  232.         custom.bltdmod = 0;
  233.         custom.bltcon1 = 0;
  234.         custom.bltcon0 = BC0F_SRCA | BC0F_SRCB | BC0F_DEST |
  235.             ABC | ANBC | ABNC | ANBNC;
  236.         custom.bltsize = mybltsize;
  237.     }
  238. /* fill planes with 1 wherever plane b is set to 1, except for title area. */
  239. #ifdef NOCYCLE
  240.     for(x = 0; x < 5; x++)
  241.     {
  242.         WaitBlit();
  243.         custom.bltapt = (APTR)(b + offset);
  244.         custom.bltbpt = (APTR)(r->BitMap->Planes[x] + offset);
  245.         custom.bltdpt = (APTR)(r->BitMap->Planes[x] + offset);
  246.         custom.bltafwm = 0xffff;
  247.         custom.bltalwm = 0xffff;
  248.         custom.bltamod = 0;
  249.         custom.bltbmod = 0;
  250.         custom.bltdmod = 0;
  251.         custom.bltcon1 = 0;
  252.         custom.bltcon0 = BC0F_SRCA | BC0F_SRCB | BC0F_DEST |
  253.             A_OR_B;
  254.         custom.bltsize = minbltsize;
  255.     }
  256. #endif 
  257.     WaitBlit();
  258.     DisownBlitter();
  259. }
  260.  
  261. fracsub(r,a,b)
  262. struct RastPort *r;
  263. PLANEPTR a,b;
  264. {
  265.     register int x,newoff;
  266.  
  267.     newoff = offset;
  268.      mybltsize = ((w1->RPort->BitMap->Rows - 10) << 6) |
  269.         (w1->RPort->BitMap->BytesPerRow >> 1);
  270.     OwnBlitter();
  271.     for(x = 0; x < 5; x++) /* for each bitplane */
  272.     {
  273.         WaitBlit();
  274. /* plane a = !plane x AND plane b */
  275.         custom.bltapt = (APTR)(b+newoff);
  276.         custom.bltbpt = (APTR)(r->BitMap->Planes[x]+newoff);
  277.         custom.bltdpt = (APTR)(a+newoff);
  278.         custom.bltafwm = 0xffff;
  279.         custom.bltalwm = 0xffff;
  280.         custom.bltamod = 0;
  281.         custom.bltbmod = 0;
  282.         custom.bltdmod = 0;
  283.         custom.bltcon1 = 0;
  284.         custom.bltcon0 = BC0F_SRCA | BC0F_SRCB | BC0F_DEST |
  285.             ANBC | ANBNC;
  286.         custom.bltsize = mybltsize;
  287. /* plane x = plane x XOR plane b */
  288.         WaitBlit();
  289.         custom.bltapt = (APTR)(b+newoff);
  290.         custom.bltbpt = (APTR)(r->BitMap->Planes[x]+newoff);
  291.         custom.bltdpt = (APTR)(r->BitMap->Planes[x]+newoff);
  292.         custom.bltafwm = 0xffff;
  293.         custom.bltalwm = 0xffff;
  294.         custom.bltamod = 0;
  295.         custom.bltbmod = 0;
  296.         custom.bltdmod = 0;
  297.         custom.bltcon1 = 0;
  298.         custom.bltcon0 = BC0F_SRCA | BC0F_SRCB | BC0F_DEST |
  299.             ANBC | NABC | ANBNC | NABNC;
  300.         custom.bltsize = mybltsize;
  301. /* plane b = plane a */
  302.         WaitBlit();
  303.         custom.bltapt = (APTR)(a+newoff);
  304.         custom.bltdpt = (APTR)(b+newoff);
  305.         custom.bltafwm = 0xffff;
  306.         custom.bltalwm = 0xffff;
  307.         custom.bltamod = 0;
  308.         custom.bltbmod = 0;
  309.         custom.bltdmod = 0;
  310.         custom.bltcon1 = 0;
  311.         custom.bltcon0 = BC0F_SRCA | BC0F_SRCB | BC0F_DEST |
  312.             ABC | ANBC | ABNC | ANBNC;
  313.         custom.bltsize = mybltsize;
  314.     }
  315. /* fill planes with 0 wherever plane a is set to 1 */
  316. #ifdef NOCYCLE
  317.     for(x = 0; x < 5; x++)
  318.     {
  319.         WaitBlit();
  320.         custom.bltapt = (APTR)(b + newoff);
  321.         custom.bltbpt = (APTR)(r->BitMap->Planes[x] + newoff);
  322.         custom.bltdpt = (APTR)(r->BitMap->Planes[x] + newoff);
  323.         custom.bltafwm = 0xffff;
  324.         custom.bltalwm = 0xffff;
  325.         custom.bltamod = 0;
  326.         custom.bltbmod = 0;
  327.         custom.bltdmod = 0;
  328.         custom.bltcon1 = 0;
  329.         custom.bltcon0 = BC0F_SRCA | BC0F_SRCB | BC0F_DEST |
  330.             NABC | NABNC ;
  331.         custom.bltsize = mybltsize;
  332.     }
  333. #endif
  334.     WaitBlit();
  335.     DisownBlitter();
  336. }
  337.  
  338. FudgeColors()
  339. {
  340.     short a, i;
  341.  
  342.     a = colormap[31];
  343.     for(i = 31; i > 0; i--) colormap[i] = colormap[i-1];
  344.     colormap[0] = a;
  345.     LoadRGB4(&s->ViewPort, colormap, 32L);
  346. }
  347.  
  348. die(kind)
  349. int kind;
  350. {
  351.     while(msg = GetMsg(w->UserPort)) ReplyMsg(msg);
  352.     while(msg = GetMsg(w1->UserPort)) ReplyMsg(msg);
  353.     while(msg = GetMsg(w2->UserPort)) ReplyMsg(msg);
  354.     while(msg = GetMsg(w3->UserPort)) ReplyMsg(msg);
  355.     if(tmpras.RasPtr) FreeRaster(tmpras.RasPtr, 320L, 200L);
  356.     if(w2) CloseWindow(w2);
  357.     if(w3) CloseWindow(w3);
  358.     if(w1) CloseWindow(w1);
  359.     if(s1) CloseScreen(s1);
  360.     if(w) CloseWindow(w);
  361.     if(s) CloseScreen(s);
  362.     if(IntuitionBase) CloseLibrary(IntuitionBase);
  363.     if(GfxBase) CloseLibrary(GfxBase);
  364.     exit(kind);
  365. }
  366.